package cz.cuni.amis.episodic.dybanem;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import java.io.PrintStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ik
 */
public class GoogleUtils {

    public static void printTable(Table t, PrintStream out) {
        printTable(t, out, "\t");
    }
    
    public static void printTable(Table t, PrintStream out, String sep) {
		NumberFormat nf = NumberFormat.getInstance();
		// print header
		out.print(sep);
		List<Object> columns = new ArrayList(t.columnKeySet());
		for (Object columnKey : columns) {
			out.append(columnKey.toString());
			out.append(sep);
		}
		out.println();

		for (Object rowKey : t.rowKeySet()) {
			out.print(rowKey.toString());
			out.print(sep);

			for (Object colKey : columns) {
				out.print(nf.format(t.get(rowKey, colKey)));
				out.print(sep);
			}
			out.println();
		}

    }
    
    public static <T> Table<Integer, String, T> toTable(List<T> list, String columnName) {
        Table<Integer, String, T> table = HashBasedTable.create();
        for(int i = 0; i < list.size(); i++) {
            table.put(i, columnName, list.get(i));
        }
        return table;
    }
    
    public static <T> Table<Integer, String, T> toTable(T[] arr, String columnName) {
        Table<Integer, String, T> table = HashBasedTable.create();
        for(int i = 0; i < arr.length; i++) {
            table.put(i, columnName, arr[i]);
        }
        return table;
    }
}
